//
//  RHPackView.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/12/5.
//

#import "RHPackView.h"

@interface RHPackView()


@property (nonatomic ,strong) QMUILabel *snLab;

@property (nonatomic ,strong) QMUILabel *statusContentLab;

@property (nonatomic ,strong) QMUILabel *softwareContentLab;

@property (nonatomic ,strong) QMUILabel *cycleContentLab;

@property (nonatomic ,strong) QMUILabel *capacityContentLab;

@property (nonatomic ,strong) UIImageView *icon;

@end
@implementation RHPackView

-(instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        [self setupUI];
    }
    return self;
}

-(void)setPackModel:(RHDevPackModel *)packModel
{
    _packModel = packModel;
    self.snLab.text = ValidNullDataToString(packModel.sn, @"--");
    //0:离线   1:在线(正常)  2:异常(不会返回)
    if(_packModel.status == 1){
        self.statusContentLab.text = @"正常".rh_localized;
        self.statusContentLab.textColor = kCOLOR_HEX(@"50D671");
    }else if(_packModel.status == 0){
        self.statusContentLab.text = @"离线".rh_localized;
        self.statusContentLab.textColor = kCOLOR_HEX_A(@"#171A1D",0.6);
    }else if(_packModel.status == 2){
        self.statusContentLab.text = @"异常".rh_localized;
        self.statusContentLab.textColor = kCOLOR_HEX(@"E52860");
    }
    self.softwareContentLab.text = ValidNullDataToString(packModel.softVersion, @"--");
    self.cycleContentLab.text = [NSString stringWithFormat:@"%ld",packModel.cycles];
    
    NSString *value = [NSDecimalNumber decimalPlacesWithRoundUpWithDigit:3 value:packModel.RSOC];
    self.capacityContentLab.text = [NSString stringWithFormat:@"%@Ah",value];
    
    CGFloat softWidth = [self.softwareContentLab.text getStringWidthFont:self.softwareContentLab.font limitHeight:20];
    CGFloat snWidth = [self.snLab.text getStringWidthFont:self.snLab.font limitHeight:20];
    CGFloat cyecleWidth = [self.cycleContentLab.text getStringWidthFont:self.cycleContentLab.font limitHeight:20];
    CGFloat capWidth = [self.capacityContentLab.text getStringWidthFont:self.capacityContentLab.font limitHeight:20];
    [self.softwareContentLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(softWidth);
    }];
    
    [self.snLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(snWidth);
    }];
    
    [self.cycleContentLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(cyecleWidth);
    }];
    
    [self.capacityContentLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(capWidth);
    }];
}

-(void)setupUI
{
    kWeakSelf;
    self.backgroundColor = CFTabViewBgColor;
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    _icon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"device_pack_normal"]];
    _icon.contentMode = UIViewContentModeScaleAspectFit;
    _icon.backgroundColor = CFTabViewBgColor;
    [bgView addSubview:_icon];
    [_icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(24);
        make.left.top.mas_offset(10);
    }];
    
    _packTitleLab = [[QMUILabel alloc]init];
    [bgView addSubview:_packTitleLab];
    _packTitleLab.text = @"Pack 1#";
    _packTitleLab.font = kSYSTEMFONT(14);
    _packTitleLab.textColor = [UIColor colorWithHexString:@"3F4443" alpha:1];
    _packTitleLab.textAlignment = NSTextAlignmentLeft;
    [_packTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.icon);
        make.left.equalTo(weakSelf.icon.mas_right).offset(5);
    }];
    
    _statusContentLab = [[QMUILabel alloc]init];
    [bgView addSubview:_statusContentLab];
    _statusContentLab.font = kSYSTEMFONT(12);
    _statusContentLab.text = @"正常".rh_localized;
    _statusContentLab.textColor = [UIColor colorWithHexString:@"50D671" alpha:1];
    _statusContentLab.textAlignment = NSTextAlignmentLeft;
    
    [_statusContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.icon);
        make.right.mas_offset(-10);
    }];
    
    UIView *detailView = kView;
    detailView.backgroundColor = CFTabViewBgColor;
    detailView.layer.cornerRadius = 8;
    detailView.layer.masksToBounds = YES;
    [bgView addSubview:detailView];
    [detailView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.icon);
        make.right.equalTo(weakSelf.statusContentLab);
        make.top.equalTo(weakSelf.icon.mas_bottom).offset(10);
        make.bottom.mas_offset(-10);
    }];
    
    QMUILabel *softwareTitleLab = [QMUILabel labelWithText:[NSString stringWithFormat:@"%@:",@"软件版本".rh_localized] font:kSYSTEMFONT(10) textColor:kCOLOR_HEX_A(@"#3F4443", 0.6)];
    softwareTitleLab.textAlignment = NSTextAlignmentLeft;
    softwareTitleLab.numberOfLines = 2;
    softwareTitleLab.adjustsFontSizeToFitWidth = YES;
    softwareTitleLab.minimumScaleFactor = 0.8;
    [detailView addSubview:softwareTitleLab];
    [softwareTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_offset(10);
    }];
    
    _softwareContentLab = [QMUILabel labelWithFont:kSYSTEMFONT(10) textColor:kCOLOR_HEX_A(@"#3F4443", 0.6) textAligment:NSTextAlignmentRight];
    _softwareContentLab.text = @"--";
    [detailView addSubview:_softwareContentLab];
    [_softwareContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(softwareTitleLab.mas_right);
        make.right.equalTo(detailView.mas_centerX).offset(-15);
        make.centerY.equalTo(softwareTitleLab);
    }];
    
    QMUILabel *snTitleLab = [QMUILabel labelWithText:[NSString stringWithFormat:@"%@:",@"SN".rh_localized] font:kSYSTEMFONT(10) textColor:kCOLOR_HEX_A(@"#3F4443", 0.6)];
    snTitleLab.textAlignment = NSTextAlignmentLeft;
    snTitleLab.numberOfLines = 2;
    [detailView addSubview:snTitleLab];
    CGFloat width = [snTitleLab.text getStringWidthFont:snTitleLab.font limitHeight:20];
    [snTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(detailView.mas_centerX).offset(15);
        make.centerY.equalTo(softwareTitleLab);
        make.width.offset(width);
    }];
    
    _snLab = [QMUILabel labelWithFont:kSYSTEMFONT(10) textColor:kCOLOR_HEX_A(@"#3F4443", 0.6) textAligment:NSTextAlignmentRight];
    _snLab.text = @"--";
    _snLab.numberOfLines = 2;
    [detailView addSubview:_snLab];
    [_snLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(snTitleLab.mas_right);
        make.right.mas_offset(-10);
        make.centerY.equalTo(softwareTitleLab);
    }];
    
    QMUILabel *cyecleTitleLab = [QMUILabel labelWithText:[NSString stringWithFormat:@"%@:",@"循环次数".rh_localized] font:kSYSTEMFONT(10) textColor:kCOLOR_HEX_A(@"#3F4443", 0.6)];
    cyecleTitleLab.textAlignment = NSTextAlignmentLeft;
    cyecleTitleLab.numberOfLines = 2;
    cyecleTitleLab.adjustsFontSizeToFitWidth = YES;
    cyecleTitleLab.minimumScaleFactor = 0.8;
    [detailView addSubview:cyecleTitleLab];
    [cyecleTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(softwareTitleLab);
        make.bottom.mas_offset(-10);
    }];
    
    _cycleContentLab = [QMUILabel labelWithFont:kSYSTEMFONT(10) textColor:kCOLOR_HEX_A(@"#3F4443", 0.6) textAligment:NSTextAlignmentRight];
    _cycleContentLab.text = @"--";
    [detailView addSubview:_cycleContentLab];
    [_cycleContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cyecleTitleLab.mas_right);
        make.right.equalTo(detailView.mas_centerX).offset(-15);
        make.centerY.equalTo(cyecleTitleLab);
    }];
    
    QMUILabel *capacityTitleLab = [QMUILabel labelWithText:[NSString stringWithFormat:@"%@:",@"剩余容量".rh_localized] font:kSYSTEMFONT(10) textColor:kCOLOR_HEX_A(@"#3F4443", 0.6)];
    capacityTitleLab.textAlignment = NSTextAlignmentLeft;
    capacityTitleLab.numberOfLines = 2;
    capacityTitleLab.adjustsFontSizeToFitWidth = YES;
    capacityTitleLab.minimumScaleFactor = 0.8;
    [detailView addSubview:capacityTitleLab];
    [capacityTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(detailView.mas_centerX).offset(15);
        make.centerY.equalTo(cyecleTitleLab);
    }];
    
    _capacityContentLab = [QMUILabel labelWithFont:kSYSTEMFONT(10) textColor:kCOLOR_HEX_A(@"#3F4443", 0.6) textAligment:NSTextAlignmentRight];
    _capacityContentLab.text = @"--";
    [detailView addSubview:_capacityContentLab];
    [_capacityContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(capacityTitleLab.mas_right);
        make.right.mas_offset(-10);
        make.centerY.equalTo(capacityTitleLab);
    }];
}

@end
